const Ajv = require('ajv');
const ajv = new Ajv();
const aq = require('../schema/answerQuestions');

const choiceQuestionAnswerVaildata = ajv.compile(aq.choiceQuestionAnswer)
const blankQuestionAnswerVaildata = ajv.compile(aq.blankQuestionAnswer)
const judgingQuestionAnswerVaildata = ajv.compile(aq.judgingQuestionAnswer)
const shortAnswerQuestionAnswerVaildata = ajv.compile(aq.shortAnswerQuestionAnswer)

function isChoiceQuestionAnswer(obj){
    return choiceQuestionAnswerVaildata(obj);
}
function isBlankQuestionAnswer(obj){
    return blankQuestionAnswerVaildata(obj)
}
function isJudgingQuestionAnswer(obj){
    return judgingQuestionAnswerVaildata(obj)
}
function isShortAnswerQuestionAnswer(obj){
    return shortAnswerQuestionAnswerVaildata(obj)
}

function isAnswerQuestionArray(arr){
    if(!Array.isArray(arr))return{
        msg:"不是数组"
    }
    for (let i = 0; i < arr.length; i++) {
        let item = arr[i];
        if(!(item?.eq_id)) return false;
        if(!isChoiceQuestionAnswer(item?.answer_body)
            &&!isBlankQuestionAnswer(item?.answer_body)
            &&!isJudgingQuestionAnswer(item?.answer_body)
            &&!isShortAnswerQuestionAnswer(item?.answer_body))
            return false;
    }
    return true;
}



function test(){
    let a = [1,2,3]
    let b = ['dsadas','das','das']
    let c = true
    let d = ''
    let result1 = isChoiceQuestionAnswer(a);
    let result2 = isBlankQuestionAnswer(b);
    let result3 = isJudgingQuestionAnswer(c);
    let result4 = isShortAnswerQuestionAnswer(d);

    let arr = [
        {
            eq_id:10,
            answer_body:d
        }
    ]
    console.log(isAnswerQuestionArray(arr));
}

test();

module.exports = {
    isChoiceQuestionAnswer,
    isBlankQuestionAnswer,
    isJudgingQuestionAnswer,
    isShortAnswerQuestionAnswer,
    isAnswerQuestionArray
}

